﻿LApp.controller('Account_LogOnCtrl', function ($scope, $rootScope, $state, $stateParams, $http, $lvl) {

    $scope.Bg_Style = {
        height: document.documentElement.clientHeight + "px"
    }

    $scope.LogOnError = '';
    $scope.LogOnIng = false;
    $scope.LogOnBtnTxt = "登   录";
    $scope.LogOnInfo = {
        UserName: localStorage.getItem("KeepLogOn_UserName") || '',
        Password: localStorage.getItem("KeepLogOn_Password") || ''
    }

    $scope.Keyup = function (e) {
        var keycode = window.event ? e.keyCode : e.which;
        if (keycode === 13) {
            $scope.Action_LogOn();
        }
    }

    $scope.Action_LogOn = function () {

        if ($scope.LogOnInfo.UserName === '') {
            $scope.LogOnError = '请输入用户名';
            return;
        }

        if ($scope.LogOnInfo.Password === '') {
            $scope.LogOnError = '请输入密码';
            return;
        }

        $scope.LogOnIng = true;
        $scope.LogOnError = '';
        $scope.LogOnBtnTxt = '正在登录...';

        $http({
            url: GlobalConfig.APIServer + "nbapi/auth/userlogon?_=" + Math.random(),
            method: 'POST',
            headers: {
                'Access-Token': window.AccessToken
            },
            params: null,
            data: $scope.LogOnInfo
        }).success(function (response, header, config, status) {
            $scope.LogOnIng = false;
            if (response.success === false) {
                $scope.LogOnBtnTxt = "登   录";
                $scope.LogOnError = response.message;
            } else {
                $scope.LogOnBtnTxt = "登录成功,正在跳转...";
                localStorage.setItem("KeepLogOn_UserName", $scope.LogOnInfo.UserName);
                localStorage.setItem("KeepLogOn_Password", $scope.LogOnInfo.Password);
                $lvl.storeItem("CurrentUser", response.result.userInfo);

                //token\sign
                localStorage.setItem("AccessToken", response.result.token);
                localStorage.setItem("AccessSign", response.result.sign);

                $state.go('Manager.NBHome');
                //$state.go('Manager.Home');
            }
        }).error(function (response, header, config, status) {
            $scope.LogOnBtnTxt = "登   录";
            $scope.LogOnIng = false;
            $scope.LogOnError = "服务器异常";
        });
    }
})

LApp.controller('ManagerCtrl', function ($scope, $rootScope, $state, $lvl) {
  

    window.AccessToken = localStorage.getItem("AccessToken");

    //$rootScope.CheckPassword();

    //$rootScope.Base_CurrentSystemFilter = function (value) {
    //    if (!$rootScope.CurrentUser) return false;
    //    if ($rootScope.CurrentUser.DepartCode === '') {
    //        return true;
    //    }
    //    if ($rootScope.CurrentUser.SystemCode) {
    //        return value.SmallCode === $rootScope.CurrentUser.SystemCode;
    //    } else {
    //        return value.MiddleCode === $rootScope.CurrentUser.DepartCode;
    //    }
    //}

    //$rootScope.Base_CurrentSystemList = [];
    //angular.forEach($rootScope.Base_SystemList, function (a) {
    //    if ($rootScope.CurrentUser.DepartCode == '') {
    //        $rootScope.Base_CurrentSystemList.push(a);
    //    }
    //    if ($rootScope.CurrentUser.SystemCode) {
    //        if (a.SmallCode == $rootScope.CurrentUser.SystemCode) {
    //            $rootScope.Base_CurrentSystemList.push(a);
    //        }
    //    } else {
    //        if (a.MiddleCode == $rootScope.CurrentUser.DepartCode) {
    //            $rootScope.Base_CurrentSystemList.push(a);
    //        }
    //    }
    //})

  


    $scope.MenuListAll = angular.copy(window.MenuList);

    $scope.MenuList = {};

    var belongList = [];
    angular.forEach($scope.MenuListAll, function (a) {
        //加载菜单
        if ($rootScope.CurrentUser) {
            $scope.MenuList[a.MenuID] = a;
            belongList.push(a.Belong);
        } else if ($rootScope.CurrentUser.RoleMenus.indexOf(a.MenuID) !== -1) {
            $scope.MenuList[a.MenuID] = a;
            belongList.push(a.Belong);
        }
    })
    var _currentMenuID = $state.current.MenuID
    angular.forEach($scope.MenuList, function (a, i) {
        if (a.MenuID === _currentMenuID) {
            a.IsActive = true;
        }
        if (a.ParentMenuID === '') {
            a.Level = "lv1";
            a.Visible = true;

            if (a.HasChild === false) {
                a.MenuIcon = "glyphicon-th";
            } else {
                if (_currentMenuID && a.MenuID === $state.current.ParentMenuID) {
                    a.MenuIcon = "glyphicon-chevron-down";
                } else {
                    a.MenuIcon = "glyphicon-chevron-up";
                }
            }
        } else {
            a.Level = "lv2";
            a.MenuIcon = "glyphicon-leaf";
            if (_currentMenuID && _currentMenuID.indexOf(a.ParentMenuID) !== -1) {
                a.Visible = true;
            } else {
                a.Visible = false;
            }
        }
    })


    $scope.MenuClick = function (item) {
        if (item.HasChild === true) {
            if (item.MenuIcon === "glyphicon-chevron-up") {
                item.MenuIcon = "glyphicon-chevron-down";
                angular.forEach($scope.MenuList, function (a, i) {
                    if (a.ParentMenuID === item.MenuID) {
                        a.Visible = true;
                    }
                })
            } else {
                item.MenuIcon = "glyphicon-chevron-up";
                angular.forEach($scope.MenuList, function (a, i) {
                    if (a.ParentMenuID === item.MenuID) {
                        a.Visible = false;
                    }
                })
            }
        } else if (item.StateName) {
            angular.forEach($scope.MenuList, function (a, i) {
                a.IsActive = false;
            })
            item.IsActive = true;
            $state.go(item.StateName, item.StateParam);
        }
    }


    $scope.BelongList = [];

    if ($lvl.inArray('DBCenter', belongList)) {
        $scope.BelongList.push({ Code: 'DBCenter', Text: 'NBMGJ' });
    }
    $scope.ChangeBelong = function (_belong, isFirst) {
        $scope.CurrentBelong = _belong;
        if (isFirst && $state.current.name !== 'Manager' && $state.current.name !== 'Manager.Home') {
            var a = 1;
        } else {
            $state.go('Manager.NBHome');
            //$state.go('Manager.' + _belong + '_' + 'Home');
        }
    }

    $scope.ChangeBelong($scope.BelongList[0].Code, true);
    
    //监听路由事件
    $scope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
        if (toState.url === "/Manager" && fromState.url !== "^") {
            WeixinJSBridge.call('closeWindow');
        }
    })
})



LApp.controller('ManagerHomeCtrl', function ($scope, $rootScope, $state, $lvl) {

})


LApp.controller("NBManagerHomeCtrl", function ($scope, $rootScope, $state, $lvl) {
    angular.extend($scope, {
        ClickOne: function () {
            $state.go('Manager.DBCenter_CommnetList')
        },
        CloseOne: function () {
           window.history.go(-1);
        },
        AllClose: function () {
            WeixinJSBridge.call('closeWindow');
        },
    })
})


//DEMO
LApp.controller('DBCenter_CustomerListCtrl', function ($scope, $rootScope, $state, $lmapHelper, $lvl, DBCenterService) {
    angular.extend($scope, {
        condition:{
            pageIndex: 1,
            pageSize:20
        },
        GetSysRegion: function (pageindex) {
            $scope.condition.pageIndex = pageindex || 1;
            DBCenterService.GetSysRegionAllData({
                data: $scope.condition,
                success: function (response) {
                    $scope.SYSRegionData = response;
                }
            })
        }
    })

    $scope.GetSysRegion();
})